package jltx.fragmentpageframework.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.annotation.ColorInt;
import android.support.annotation.ColorRes;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.StaticLayout;
import android.util.AttributeSet;
import android.widget.TextView;

import jltx.fragmentpageframework.R;

/**
 * @author jltxseo
 *         Created by junlintianxia on 2017/06/21.
 */

@SuppressLint("AppCompatCustomView")
public class ChangeColorTextView extends TextView{
    private int selectMskColor;
    private int unSelectMskColor;
    private float offset = 0;  //0....1

    private Paint mTextPaint;
    private float mFontBaseLine; //文字是从基准开始画
    private float mTextWidth;

    public ChangeColorTextView(Context context) {
        super(context);
        initArray(context,null);
    }

    public ChangeColorTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initArray(context,attrs);
    }

    public ChangeColorTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initArray(context,attrs);
    }

    private void initArray(Context context,AttributeSet attrs){
        selectMskColor = ContextCompat.getColor(context, R.color.colorGreenWeChat);
        unSelectMskColor = ContextCompat.getColor(context, R.color.colorGrayWeChat);
        TypedArray a = context.obtainStyledAttributes(attrs,R.styleable.ChangeColorView);
        int n = a.getIndexCount();

        for(int i=0; i < n; i++){
            int attr = a.getIndex(i);
            switch (attr){
                case R.styleable.ChangeColorView_selectMaskColor:
                    selectMskColor = a.getColor(attr,selectMskColor);
                    break;
                case R.styleable.ChangeColorView_unSelectMaskColor:
                    unSelectMskColor = a.getColor(attr,unSelectMskColor);
                    break;
            }
        }

        a.recycle();

        mTextPaint = new Paint();
        mTextPaint.setAntiAlias(true);
        Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        mFontBaseLine = (float) Math.ceil(fontMetrics.descent -fontMetrics.ascent);
        mTextWidth = StaticLayout.getDesiredWidth(getText(), getPaint());

    }

    public  void setBottomText(CharSequence text) {
        setText(text);
        Paint.FontMetrics fontMetrics = getPaint().getFontMetrics();
        mFontBaseLine = (float) Math.ceil(fontMetrics.descent -fontMetrics.ascent);
        mTextWidth = StaticLayout.getDesiredWidth(getText(), getPaint());
        invalidate();
    }

    public void setIconBottomTextColor(@ColorInt int unselectColor, @ColorInt int selectColor){

        unSelectMskColor = unselectColor;
        selectMskColor = selectColor;
        invalidate();
    }

    public void setIconBottomTextColorRes(@ColorRes int unselectColor, @ColorRes int selectColor){
        unSelectMskColor = getResources().getColor(unselectColor);
        selectMskColor = getResources().getColor(selectColor);
        invalidate();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawUnSelectText(canvas);
        drawSelectText(canvas);
    }

    private void drawUnSelectText(Canvas canvas){
        mTextPaint.setColor(unSelectMskColor);
        mTextPaint.setAlpha((int)(255 * (1 - offset)));
        mTextPaint.setTextSize(getTextSize());
        canvas.drawText(getText().toString(),(getWidth() - mTextWidth) / 2,  mFontBaseLine,mTextPaint);
    }

    private void drawSelectText(Canvas canvas){
        mTextPaint.setColor(selectMskColor);
        mTextPaint.setAlpha((int)(255 * offset));
        mTextPaint.setTextSize(getTextSize());
        canvas.drawText(getText().toString(),(getWidth() - mTextWidth) / 2, mFontBaseLine,mTextPaint);
    }

    /**
     * 改变TextView蒙层颜色
     * @param offset
     */
    /**
     * 改变ImageView的蒙层颜色
     * @param offset
     */
    public void changeImageViewDrawable(float offset){
        if(this.offset != offset){
            this.offset = offset;
            invalidate();
        }
    }
}
